import { Injectable } from '@angular/core';
import { Storage } from './Storage';
import { Logger } from './Logger';
import { Helper } from './Helper';
import { NavController } from '@ionic/angular';

export declare interface RequestSetting {
    /**
     * 是否使用默认api
     */
    useDefaultApi?: boolean;
    /**
     * 请求结果是否需要缓存
     */
    needCache?: boolean;
    /**
     * 是否显示loading
     */
    showLoading?: boolean;
}


/**
 * http工具类
 */
@Injectable({
    providedIn: 'root'
})
export class HttpHelper {
    requestCount: number = 0; // 记录未完成的请求数量,当请求数为0关闭loading,当不为0显示loading

    constructor(public helper: Helper, public nav: NavController) {

    }

    static getDefaultSetting(setting) {
        const defaultSetting: RequestSetting = {
            useDefaultApi: true,
            needCache: false,
            showLoading: true,
        };
        return setting ? { ...defaultSetting, ...setting } : defaultSetting;
    }

    static getCacheData(options) {
        const cacheKey = HttpHelper.getCacheKey(options);
        return Storage.sessionStorage.get(cacheKey) || null;
    }

    static setCacheData(options, data: any) {
        const cacheKey = HttpHelper.getCacheKey(options);
        Storage.sessionStorage.set(cacheKey, data);
    }

    static getCacheKey(options) {
        const strParams = JSON.stringify(options.params);
        const strBody = JSON.stringify(options.body);
        return options.url + strParams + strBody;
    }

    requestBefore(options, setting: RequestSetting) {
        this.showLoading(setting);
        Logger.log('请求前', '#3880ff', 'options:', JSON.stringify(options));
    }

    requestSuccess(options, isShow: boolean = true) {
        console.log('isShow======', isShow);
        if (isShow) {
            this.hideLoading();
        }
        Logger.log('请求成功', '#10dc60', 'options:', options);
    }

    requestError(options, isLogger: boolean = true) {
        this.hideLoading();
        if (isLogger) {
            Logger.log('请求失败', '#f04141', 'options:', options);
        } else {
            this.helper.alert('Show', "Please Login", res => {
                this.nav.navigateForward('login');
            });
            Logger.log('未登录', '#f04141', 'options:', '当前未登录');
        }
    }

    private showLoading(setting: RequestSetting) {
        console.log('this.requestCount===========', this.requestCount);
        if (setting.showLoading) {
            // if (this.requestCount == 0) {
                this.helper.showLoadingMsg();
            // }
            this.requestCount++;
        }
        // if (setting.showLoading) {
        //     ++HttpHelper.requestCount;
        //     // if (HttpHelper.requestCount > 10) {
        //     //     HttpHelper.requestCount = 1;
        //     // }
        //     if ( setting.showLoading) {
        //         this.helper.showLoading();
        //     }
        //     // setting.showLoading &&
        // }
    }

    private hideLoading() {
        if (this.requestCount > 1) {
            this.requestCount--;
            if (this.requestCount == 1) {
                this.helper.hideLoadingMsg();
                console.log('this.requestCount===', this.requestCount);
            }
        } else {
           
            this.helper.hideLoadingMsg();
        }
        // let iConunt=--HttpHelper.requestCount;
        // if (iConunt < 0) {
        //     HttpHelper.requestCount=0;
        //     this.helper.hideLoading();
        // }else {
        //     this.helper.hideLoading()
        //     --HttpHelper.requestCount === 0 && this.helper.hideLoading();
        // }
    }




}
